﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dm;
using System.Data.SqlClient;

namespace OneClick
{
    public partial class 列统计信息 : Form
    {
        static DmConnection cnn = new DmConnection();
        public 列统计信息()
        {
            InitializeComponent();
        }

        private void 列统计信息_Load(object sender, EventArgs e)
        {
            cnn.ConnectionString = Form1.sstr;
            string COL = Form1.Clo;
            string Tab = Form1.Tab;
            string Sch = Form1.Sch;
            decimal num_1 = Form1.num_1;
            this.Text = Form1.Comment + "=>统计信息"; // 修改窗口标题
            label1.Text = (Sch + '.' + Tab + '.' + COL);

            string sql2 = "dbms_stats.column_stats_show(upper('" + Sch + "'),upper('" + Tab + "'),upper('" + COL + "'))";
            //label1.Text = (sql2);
            cnn.Open();
            DmCommand command = new DmCommand();
            command.Connection = cnn;
            try
            {
                string a;
                string b;
                string c;
                string d;
                command.CommandText = sql2;
                //读第一个结果集
                DmDataReader reader = command.ExecuteReader();
                //listBox 只有一行 改成 lable2 // while (listBox1.Items.Count > 0)
                //listBox 只有一行 改成 lable2 // {
                //listBox 只有一行 改成 lable2 //     listBox1.Items.Remove(listBox1.Items[0]);
                //listBox 只有一行 改成 lable2 // }

                while (reader.Read())
                {
                    a = reader.GetString(0);
                    b = reader.GetString(3);
                    c = reader.GetString(4);
                    if (c == "") { c = "NULL"; }
                    d = reader.GetString(5);
                    if (d == "")
                    {
                        c = "NULL";
                        label1.Text = label1.Text + "实际量:" + d;
                    }
                    else
                    {
                        decimal dd = Convert.ToInt32(d);
                        label1.Text = label1.Text + "    实际量:" + num_1 + "    统计量:" + dd + "    统计差额比: " + Math.Round(Math.Abs(num_1 - dd) / dd, 4);//保留四位小数
                    }
                    //listBox 只有一行 改成 lable2 //listBox1.Items.Add("不同列值的个数: " + a + "\t"+ "空值的个数: " + b + "\t" + "直方图桶的个数: " + c + "\t" + "样本容量: " + d); ;
                    label2.Text = ("不同列值的个数: " + a + "    " + "空值的个数: " + b + "    " + "直方图桶的个数: " + c + "    " + "样本容量: " + d); // label 中 不认  \t 用 四个空格代替
                }

                reader.NextResult();
                //读第二个结果集
                DataTable dt = new DataTable();
                dt.Columns.Add("value", typeof(String));
                dt.Columns.Add("<value_nums<", typeof(Int64));
                dt.Columns.Add("tvalue_nums", typeof(Int64));
                dt.Columns.Add("<values_distinct_nums<", typeof(Int64));
                while (reader.Read())
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = reader.GetString(4);
                    dr[1] = reader.GetInt64(5);
                    dr[2] = reader.GetInt64(6);
                    dr[3] = reader.GetInt64(7);
                    dt.Rows.Add(dr);
                }
                this.dataGridView1.DataSource = dt;

                reader.Close();
            }
            catch (Exception ex)
            {
                label1.Text = (ex.Message);
            }
            cnn.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);



        }
    }
}
